package aynu.hd.Login.LoginService.Impl;

import aynu.hd.Config.SendeMail;
import aynu.hd.Login.LoginDao.loginDao;
import aynu.hd.Login.LoginService.loginService;
import aynu.hd.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;

@Service
public class LoginServiceImpl implements loginService {
    @Autowired
    loginDao loginDao;

    @Autowired
    SendeMail sendeMail;

    //登录验证
    @Transactional
    @Override
    public String queryUserByName(User user, HttpSession session) {
        User user1 = loginDao.queryUserByName(user.getUser_name());
        if (user1 == null) {
            return "ERROR";
        }
        if (!user1.getUser_password().equals(user.getUser_password())) {
            return "FAIL";
        }
        session.setAttribute("user", user1);
        if(user1.getUser_admin().equals("管理员")){
            return "ADMIN";
        }
        return "OK";
    }

    //获取验证码
    @Transactional
    @Override
    public String queryUserByEmail(String user_email, HttpSession session) {
        User user = loginDao.queryUserByEmail(user_email);
        if (user == null) {
            int verificationCode = (int) ((Math.random() * 9 + 1) * 100000);
            try {
                sendeMail.sendMail(user_email, verificationCode);
                System.out.println("==========邮件验证码===========" + verificationCode + "============");
                session.setAttribute("code", String.valueOf(verificationCode));
            } catch (Exception e) {
                e.getStackTrace();
            }
            return "OK";
        }
        return "ERROR";
    }

    //注册验证
    @Transactional
    @Override
    public String addUser(User user, String code, HttpSession session) {
        String code1 = (String) session.getAttribute("code");
        if (code1 == null || "".equals(code1)) {
            return "LOSE";
        }
        if (!code.equals(code1)) {
            return "FAIL";
        }
        User user1 = loginDao.queryUserByName(user.getUser_name());
        if (user1 != null) {
            return "ERROR";
        }
        User user2 = loginDao.queryUserByIdentityCard(user.getUser_identity_card());
        if (user2 != null) {
            return "CARD";
        }
        loginDao.addUser(user);
        session.removeAttribute("code");
        return "OK";
    }
    //获得找回密码验证码
    @Transactional
    @Override
    public String queryUserByEmailForget(String user_email, String user_name, HttpSession session) {
        User user = loginDao.queryUserByEmail(user_email);
        if(user==null){
            return "ERROR";
        }else {
            if(user.getUser_name().equals(user_name)){
                int verificationCode = (int) ((Math.random() * 9 + 1) * 100000);
                try {
                    sendeMail.sendMail(user_email, verificationCode);
                    System.out.println("==========忘记邮件验证码===========" + verificationCode + "============");
                    session.setAttribute("codeForget", String.valueOf(verificationCode));
                } catch (Exception e) {
                    e.getStackTrace();
                }
                return "OK";
            }else {
                return "FAIL";
            }
        }
    }
    //更改新密码
    @Transactional
    @Override
    public String userLoginForget(User user, String code, HttpSession session) {
        String code1 = (String) session.getAttribute("codeForget");
        if (code1 == null || "".equals(code1)) {
            return "LOSE";
        }
        if (!code.equals(code1)) {
            return "ERROR";
        }
        User user1 = loginDao.queryUserByEmail(user.getUser_email());
        if(user1!=null && user.getUser_name().equals(user1.getUser_name())){
            loginDao.modifyUserPasswordByname(user);
            session.removeAttribute("codeForget");
            return "OK";
        }else {
            return "FAIL";
        }
    }
}
